<template>
  <div>
    <a-card :bodyStyle="tstyle" :bordered="false">
      <div v-if="hasPerm('property:page')" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="连接名：">
                <a-input v-model="queryParam.connectName" allow-clear placeholder="请输入连接名" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :alert="true"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template v-if="hasPerm('property:add')" slot="operator" class="table-operator">
          <a-button v-if="hasPerm('property:add')" icon="plus" type="primary" @click="$refs.addForm.add()">
            新增
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('property:edit')" @click="$refs.addForm.edit(record)">编辑</a>
          <a-divider v-if="hasPerm('property:edit') & hasPerm('property:delete')" type="vertical" />
          <a-popconfirm
            v-if="hasPerm('property:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => datasourceDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </a-card>
    <add-form ref="addForm" @ok="handleOk"></add-form>
  </div>
</template>
<script>

import { STable } from '@/components'
import addForm from './addForm'
import { datasourceDelete, datasourcePage } from '@/api/modular/main/knowl/data_source/dataSourceManager'

export default {
  components: {
    STable,
    addForm
  },
  props: {
    queryParam: {
      type: Object,
      default: function() {
        return {}
      }
    },
    modal: {
      type: Boolean,
      default: function() {
        return false
      }
    }
  },
  data() {
    return {
      // 表头
      columns: [
        {
          title: '连接名称',
          align: 'center',
          dataIndex: 'connectName',
          ellipsis: true
        },
        {
          title: '数据源类型',
          align: 'center',
          dataIndex: 'datasourceType',
          ellipsis: true
        },
        {
          title: '数据库名称',
          align: 'center',
          dataIndex: 'databaseName',
          ellipsis: true
        },
        {
          title: '是否中心仓库',
          align: 'center',
          dataIndex: 'isWarehouse',
          ellipsis: true,
          customRender: function(t, r, index) {
            if (r.isWarehouse) {
              return parseInt(r.isWarehouse) === 1 ? '是' : '否'
            }
            return '是'
          }
        },
        {
          title: 'IP地址',
          align: 'center',
          dataIndex: 'ip'
        },
        {
          title: '端口',
          align: 'center',
          dataIndex: 'port'
        },
        {
          title: '用户名',
          align: 'center',
          dataIndex: 'username',
          ellipsis: true
        },
        {
          title: '密码',
          align: 'center',
          dataIndex: 'password',
          ellipsis: true
        },
        {
          title: '创建日期',
          align: 'center',
          dataIndex: 'createTime',
          ellipsis: true,
          customRender: function(t, r, index) {
            return r.createTime.split(' ')[0]
          }
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return datasourcePage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {
    if (this.hasPerm('property:edit') || this.hasPerm('property:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    tableReload() {
      this.handleOk()
    },
    datasourceDelete(record) {
      datasourceDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
<style lang="less" scoped>
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
